$(function () {

    // 拼接select选项
    $.get('/admin/category/list', function (res) {
        const data = res.data;
        const html = template('art_category', { data });
        $('#category').html(html)

    })

    // layui 日期选择
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#articleDate' //指定元素
        });
    });

    // 选择图片
    const formdata = new FormData()
    changeCoverImage = (file) => {
        formdata.append('cover', file[0])
    }

    // 发布/草稿
    saveArticle = state => {
        addRequest(state)
    }

    function addRequest(state) {
        const title = $('#inputTitle').val()
        const categoryId = +$('#category').val()
        const date = $('#articleDate').val()
        const content = $('#articleContent').val()
        formdata.append('title', title)
        formdata.append('categoryId', categoryId)
        formdata.append('date', date)
        formdata.append('content', content)
        formdata.append('state', state)
        // 发布文章
        $.ajax({
            url: '/admin/article/publish',
            type: 'post',
            data: formdata,
            contentType: false,
            processData: false,
            success(res) {
                console.log(res);
            }
        })

    }

})